package com.rrd.hive.udf.sms;

import cn.hutool.core.util.ReUtil;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;

import java.util.List;

@Description(
        name = "sms_get_org",
        value = "_FUNC_(ip) - smsFeatureService Get org of sms",
        extended = "Example:\n> SELECT _FUNC_('861824121234', 'smsbody-代发工资-代发人民币12元');\n  代发工资"
)
public class GetSmsOrgUDF extends UDF {

    private static List<String> phoneNumberPrefixList = Lists.newArrayList("134","135","136","137","138","139","147","148","150","151","152","157","158",
            "159","172","178","182","183","184","187","188","195","197","198","130","131","132","155","156","166","167","175","176","185","186","196","133","149"
            ,"153","173","177","180","181","189","190","191","193","199","192");

    public static Configuration config() {
        System.setProperty("HADOOP_USER_NAME", "hdfs");
        Configuration conf = new Configuration();
        conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
        return conf;
    }

    public static void main(String[] args) throws Exception {
        GetSmsOrgUDF dfu = new GetSmsOrgUDF();
        Text res = dfu.evaluate("861824121234", "[代发工资]-[sms]-代发人民币12元");
        if(res != null) {
            System.out.println("res: "+ res);
        } else {
            System.out.println("null");
        }
    }

    public Text evaluate(String phoneNum, String smsBody) {
        Text ret = new Text();
        String res = null;
        try {
            if(isMobile(phoneNum)) {
                res = "["+phoneNum+"]";
            } else {
                res = getSmsOrg(smsBody);
            }
        } catch (Exception e) {
            System.out.println("Excep: " + e.getMessage());
            e.printStackTrace(System.out);
        } finally {
            if(res != null) {
                ret.set(res);
            } else {
                ret = null;
            }
            return ret;
        }
    }

    public static String getSmsOrg(String body){
        // 不包含【 [的直接过滤
        if(!body.contains("【") && !body.contains("[")){
            return null;
        }
        // 有两组以上的中括号过滤
        if(ReUtil.findAllGroup0("【",body).size() > 2
                || ReUtil.findAllGroup0("\\[",body).size() > 2){
            return null;
        }
        // 匹配中括号内文字,并过滤包含活动|温馨|友情|提醒|提示|公告|通知|告知|通报
        List<String> orgs = Lists.newArrayList(Iterables.concat(ReUtil.findAllGroup0("(?<=【)(.*?)(?=】)",body),ReUtil.findAllGroup0("(?<=\\[)(.*?)(?=\\])",body)));
        return orgs.stream().filter(s -> !ReUtil.contains("活动|温馨|友情|提醒|提示|公告|通知|告知|通报|" +
                "公益短信|公安局|公安厅|财政局|管理局|税务|工联数据企业信用档案库|断卡|固话|宽带|" +
                "话费|停机|停水|停电|电费|水费|煤气|燃气|物业|车费|ETC|电视|保单|移动|联通|电信|套餐|营业厅|手机账户|医保|社保|医疗保险|社会保险|详询",s)).findFirst().orElse(null);
    }

    private Boolean isMobile(String phoneNumber){
        boolean ret = false;
        if(phoneNumber != null) {
            // 号码过滤
            ret =  (phoneNumber.length() == 11 && phoneNumberPrefixList.contains(phoneNumber.substring(0, 3))) ||
                    (phoneNumber.startsWith("86") && phoneNumber.length() >= 5 && phoneNumberPrefixList.contains(phoneNumber.substring(2, 5)) && phoneNumber.split("86")[1].length() == 11) ||
                    (phoneNumber.startsWith("+86") && phoneNumber.length() >= 6 && phoneNumberPrefixList.contains(phoneNumber.substring(3, 6)) && phoneNumber.split("\\+86")[1].length() == 11);
        }
        return ret;
    }
}
